001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Feb 27, 2003
005     * Time: 1:52:09 PM
006     */
007    
008    package EVolve.visualization;
009    
010    import EVolve.util.painters.shapes.*;
011    import EVolve.util.painters.shapes.Shape;
012    import EVolve.exceptions.NoDataPlotException;
013    
014    import java.awt.*;
015    import java.awt.image.BufferedImage;
016    import java.util.*;
017    
018    public class AutoShapeImage extends AutoImage{
019        private Shape defaultShape;
020    
021        public AutoShapeImage() {
022            w = h = 0;
023            wMax = hMax = 200;
024            image = new short[wMax][hMax];
025            for (int i = 0; i < wMax; i++) {
026                for (int j = 0; j < hMax; j++) {
027                    image[i][j] = 0;
028                }
029            }
030            defaultShape = null;
031        }
032    
033        public AutoShapeImage(Shape defaultShape) {
034            this();
035            this.defaultShape = defaultShape;
036        }
037    
038        private void drawShapes(Graphics2D g2, int x, int y) {
039            if (defaultShape == null) {
040                ArrayList value = (ArrayList)Int2Object.get(new Integer(image[x][y]));
041                for (int k=0; k<value.size(); k++)
042                    ((Shape)value.get(k)).draw(g2);
043            } else { //when there is active default shape, image array contains color
044                int width = defaultShape.getWidth(), height = defaultShape.getHeight();
045                defaultShape.setColor((Color)Int2Object.get(new Integer(image[x][y])));
046                defaultShape.move(x*width,y*height);
047                defaultShape.draw(g2);
048                defaultShape.fill(g2);
049            }
050        }
051    
052        public void setColor(int x, int y, Object shape) {
053    
054            if ((x >= wMax) || (y >= hMax)) {
055                while (x >= wMax) {
056                    wMax += 200;
057                }
058                while (y >= hMax) {
059                    hMax += 200;
060                }
061    
062                short[][] newImage = new short[wMax][hMax];
063                for (int i = 0; i < wMax; i++) {
064                    for (int j = 0; j < hMax; j++) {
065                        newImage[i][j] = 0;
066                    }
067                }
068                for (int i = 0; i < w; i++) {
069                    for (int j = 0; j < h; j++) {
070                        newImage[i][j] = image[i][j];
071                    }
072                }
073                image = newImage;
074            }
075    
076            if (defaultShape == null) {
077                Shape aShape = (Shape)shape;
078                if (image[x][y] == 0) {
079                    short size = (short)object2Int.size();
080                    ArrayList value = new ArrayList();
081                    object2Int.put(value, new Integer(size));
082                    Int2Object.put(new Integer(size),value);
083                    image[x][y] = size;
084                }
085                ((ArrayList)Int2Object.get(new Integer(image[x][y]))).add(aShape);
086            } else {
087                if (object2Int.containsKey(shape)) {
088                    image[x][y] = ((Integer)object2Int.get(shape)).shortValue(); //in this case, shape contains a color object
089                } else {
090                    short size = (short)object2Int.size();
091                    object2Int.put(shape,new Integer(size));
092                    Int2Object.put(new Integer(size),shape);
093                    image[x][y] = size;
094                }
095            }
096    
097            if (x >= w) {
098                w = x + 1;
099            }
100            if (y >= h) {
101                h = y + 1;
102            }
103        }
104    
105        public BufferedImage getImage() throws NoDataPlotException{
106            BufferedImage returnVal = null;
107    
108            if (defaultShape != null) {
109                imageWidth = defaultShape.getWidth() * w;
110                imageHeight = defaultShape.getHeight() * h;
111            }
112    
113            if (imageWidth*imageHeight == 0) throw new NoDataPlotException();
114    
115            returnVal = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
116            returnVal.getGraphics().setColor(Color.white);
117            returnVal.getGraphics().fillRect(0, 0, imageWidth, imageHeight);
118            Graphics2D g2 = (Graphics2D)returnVal.getGraphics();
119    
120            for (int i = 0; i < w; i++) {
121                for (int j = 0; j < h; j++) {
122                    if (image[i][j] != 0)
123                        drawShapes(g2,i,j);
124                }
125            }
126    
127            return returnVal;
128        }
129    
130        public Object getColor(int x, int y) {
131            if (((image!=null) && ((x < 0) || (y < 0) || (x >= image.length) || (y >= image[0].length))) ||(image[x][y]==0)) {
132                return null;
133            } else {
134                if (defaultShape == null)
135                    return Int2Object.get(new Integer(image[x][y]));
136                else
137                    return (Color)Int2Object.get(new Integer(image[x][y]));
138            }
139        }
140    
141        public Color getSortedColor(ReferenceDimension xAxis, ReferenceDimension yAxis,int x,int y) {
142            int xMappedId, yMappedId;
143            xMappedId = (xAxis == null) ? x : xAxis.getOriginMappedId(x);
144            yMappedId = (yAxis == null) ? y : yAxis.getOriginMappedId(y);
145    
146            if ((xMappedId >= wMax) || (yMappedId >= hMax) || (xMappedId < 0) || (yMappedId < 0))
147                return null;
148            else
149                if (defaultShape == null)
150                    return ((Shape)((ArrayList)Int2Object.get(new Integer(image[xMappedId][yMappedId]))).get(0)).getColor();
151                else
152                    return (Color)Int2Object.get(new Integer(image[x][y]));
153        }
154    
155        public Shape getEntityShapes(int x,int y) {
156            for (int i=0; i<w; i++) {
157                for (int j=0; j<h; j++) {
158                    ArrayList data = (ArrayList)Int2Object.get(new Integer(image[i][j]));
159                    if (data != null) {
160                        for (int k=0; k<data.size(); k++) {
161                            Shape aShape = (Shape)data.get(k);
162                            if (aShape.insideShape(x,y)) return aShape;
163                        }
164                    }
165                }
166            }
167            return null;
168        }
169    
170        public AutoImage getSortedImage(ReferenceDimension xAxis, ReferenceDimension yAxis) {
171            AutoShapeImage returnVal = new AutoShapeImage(defaultShape);
172            for (int i = 0; i < w; i++) {
173                for (int j = 0; j < h; j++) {
174                    int x, y;
175                    if (xAxis == null) {
176                        x = i;
177                    } else {
178                        x = xAxis.getSortedIndex(i);
179                    }
180                    if (yAxis == null) {
181                        y = j;
182                    } else {
183                        y = yAxis.getSortedIndex(j);
184                    }
185                    if ((x != -1) && (y != -1) && (image[i][j]!=0)) {
186                        if (defaultShape == null) {
187                            ArrayList value = (ArrayList)Int2Object.get(new Integer(image[i][j]));
188                            for (int k=0; k< value.size(); k++) {
189                                Shape aShape = (Shape)(value.get(k));
190                                returnVal.setColor(x, y, aShape);
191                            }
192                        } else {
193                            returnVal.setColor(x,y,Int2Object.get(new Integer(image[i][j])));
194                        }
195                    }
196                }
197            }
198            returnVal.setImageWidth(imageWidth,imageHeight);
199            return returnVal;
200        }
201    
202        public void setImageWidth(int width, int height) {
203            imageWidth = width;
204            imageHeight = height;
205        }
206    
207        public void reArrange(int width, int boxSize) {
208            int newWidth = width/boxSize;
209            int newHeight = ((w*h)%newWidth == 0) ? w*h/newWidth : w*h/newWidth + 1;
210            short newImage[][] = new short[newWidth][newHeight];
211            for (int i=0; i<w*h; i++) {
212                newImage[i%newWidth][i/newWidth] = image[i%w][i/w];
213            }
214    
215            w = newWidth;
216            h = newHeight;
217            image = null;
218            image = newImage;
219            defaultShape.setSize(boxSize, boxSize);
220        }
221    
222        public Object clone() {
223            AutoShapeImage o = new AutoShapeImage();
224    
225            o.image = new short[wMax][hMax];
226            o.imageWidth = imageWidth;
227            o.imageHeight = imageHeight;
228            o.Int2Object = new HashMap();
229            o.object2Int = new HashMap();
230            o.w = w;
231            o.h = h;
232            o.wMax = wMax;
233            o.hMax = hMax;
234    
235            if (defaultShape == null) {
236                o.object2Int.put(null,new Integer(0));
237                o.Int2Object.put(new Integer(0),null);
238                for (int i=0; i<image.length; i++) {
239                    for (int j=0; j<image[i].length; j++) {
240                        o.image[i][j] = image[i][j];
241                        if (image[i][j] != 0) {
242                            ArrayList value = (ArrayList)Int2Object.get(new Integer(image[i][j]));
243                            ArrayList newValue = new ArrayList();
244                            o.Int2Object.put(new Integer(image[i][j]),newValue);
245                            o.object2Int.put(newValue, new Integer(image[i][j]));
246                            for (int k=0; k<value.size(); k++) {
247                                newValue.add(((Shape)value.get(k)).clone());
248                            }
249                        } else
250                            o.image[i][j] = 0;
251                    }
252                }
253            } else {
254                o.defaultShape = (Shape)defaultShape.clone();
255                for (int i=0; i<image.length; i++) {
256                    for (int j=0; j<image[i].length; j++)
257                        o.image[i][j] = image[i][j];
258                }
259    
260                Iterator it = object2Int.keySet().iterator();
261                while (it.hasNext()) {
262                    Object key = it.next();
263                    o.object2Int.put(key, object2Int.get(key));
264                }
265                it = Int2Object.keySet().iterator();
266                while (it.hasNext()) {
267                    Object key = it.next();
268                    o.Int2Object.put(key, Int2Object.get(key));
269                }
270            }
271    
272            return o;
273        }
274    }